package com.zq.service.impl;

import com.zq.entity.Blog;
import com.zq.entity.Tag;
import com.zq.entity.Type;
import com.zq.entity.User;
import com.zq.repository.BlogRepository;
import com.zq.service.BlogService;
import com.zq.utils.FenyeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.Tuple;
import javax.persistence.criteria.*;
import java.rmi.server.UID;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author:任人渐疏
 * @Date:Created in 3:57 下午 2020/6/27
 * Update:true
 */
@Service
public class BlogServiceImpl implements BlogService {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private BlogService blogService;
    @Autowired
    private BlogRepository blogRepository;

    @Override
    public FenyeUtil<Blog> findAllList(Blog blog, Integer page, Integer typeid, Integer tagid, Integer userid) {
        FenyeUtil<Blog> f = new FenyeUtil<>();
        // Sort sort=new Sort(Sort.Direction.DESC,"createtime");
        PageRequest of = PageRequest.of(page - 1, 10, Sort.by(Sort.Direction.DESC, "createtime"));
        Page<Blog> all = blogRepository.findAll(get(blog, typeid, tagid, userid), of);
        f.setCount((int) all.getTotalElements());
        f.setData(all.getContent());
        f.setCode(0);
        f.setMsg("");
        return f;
    }

    private Specification<Blog> get(Blog blog, Integer typeid, Integer tagid, Integer userid) {
        return new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate conjunction = criteriaBuilder.conjunction();
                List<Expression<Boolean>> expressions = conjunction.getExpressions();
                if (!StringUtils.isEmpty(blog.getContent())) {
                    expressions.add(criteriaBuilder.like(root.get("content"), "%" + blog.getContent() + "%"));
                }
                if (!StringUtils.isEmpty(blog.getCreatetime())) {
                    expressions.add(criteriaBuilder.like(root.get("createtime"), "%" + blog.getCreatetime() + "%"));
                }
                if (!StringUtils.isEmpty(typeid)) {
                    expressions.add(criteriaBuilder.equal(root.get("type").get("tid"), typeid));
                }
                if (!StringUtils.isEmpty(tagid)) {
                    expressions.add(criteriaBuilder.equal(root.get("tag").get("tagid"), tagid));
                }
                if (!StringUtils.isEmpty(userid)) {
                    expressions.add(criteriaBuilder.equal(root.get("user").get("uid"), userid));
                }
                return conjunction;
            }
        };
    }

    @Override
    public String addBlog(Blog blog, Integer typeid, Integer userid) {
        Type type = new Type();
        if (!StringUtils.isEmpty(typeid)) {
            type.setTid(typeid);
            blog.setType(type);
        }
//        Tag tag = new Tag();
//        if (!StringUtils.isEmpty(tagid)) {
//            tag.setTagid(tagid);
//        }
        User user = new User();
        if (!StringUtils.isEmpty(userid)) {
            user.setUid(userid);
            blog.setUser(user);
        }

        blog.setCreatetime(sdf.format(new Date()));
        try {
            blogRepository.save(blog);
            return "添加成功";
        } catch (Exception e) {
            return "添加失败";
        }
    }

    @Override
    public String updateBlog(Blog blog ) {

        blog.setUpdatetime(sdf.format(new Date()));
        try {
            blogRepository.save(blog);
            return "修改成功";
        } catch (Exception e) {
            return "修改失败";
        }
    }

    @Override
    public String deleteBlog(String blogid) {
        try {
            String[] split = blogid.split(",");
            for (int i = 0; i < split.length; i++) {
                Integer id = Integer.parseInt(split[i]);
                blogRepository.deleteById(id);
            }
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }
}
